/*
APPENDIX TABLE 3 
Robustness of Association between Firm-Level Exposure to PSL Mandates and Workers' reports of Access to PSL in non-Mandate Places to Alternative Treatment of PTO States
*/

cd "$exhibits_path"

use "$data_path/main_analysis_file.dta", clear


* defining appendmodels program for table creation
capture program drop appendmodels
	program appendmodels, eclass
	// using first equation of model
		   syntax namelist
		   tempname b V tmp
		   foreach name of local namelist {
			   qui est restore `name'
			   mat `tmp' = e(b)
			   local eq1: coleq `tmp'
			   gettoken eq1 : eq1
			   mat `tmp' = `tmp'[1,"`eq1':"]
			  local cons = colnumb(`tmp',"_cons")
			  if `cons'<. & `cons'>1 {
				  mat `tmp' = `tmp'[1,1..`cons'-1]
			  }
			  mat `b' = nullmat(`b') , `tmp'
			  mat `tmp' = e(V)
			  mat `tmp' = `tmp'["`eq1':","`eq1':"]
			  if `cons'<. & `cons'>1 {
				  mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
			  }
			  capt confirm matrix `V'
			  if _rc {
				  mat `V' = `tmp'
			  }
			  else {
				  mat `V' = ///
				 ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
				 ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
			 }
		 }
		 local names: colfullnames `b'
		 mat coln `V' = `names'
		 mat rown `V' = `names'
		 eret post `b' `V'
		 eret local cmd "whatever"
	end



* defining controls
	global demog i.gender i.race2_harm c.age_text_clean i.kids i.eslhome i.enrolled i.cohabstatus
	global work i.manager i.longwork_yrs i.union c.hourwage_clean c.usualhours_clean 
	global statechars c.unionmem_pc c.unemp_rate  c.minwage_state  	c.race_black c.race_amind c.race_asian c.race_pacisl c.race_other c.race_multi c.hispan c.age_u5 c.age_5to9 c.age_10to14 c.age_75to84 c.age_85plus c.female_pc c.educ_pc_hsgrad c.educ_pc_somecoll c.educ_pc_assoc c.educ_pc_bach c.educ_pc_grad
	global empchars i.shift_ind c.total_emps c.total_ests

	
	
	*creating second versions of key regressors for table structure
	cap g pct_emps_psl_state_v2 = pct_emps_psl_state
	cap g pct_emps_psl_place_v2 = pct_emps_psl_place
	cap g pct_emps_psl_pto_state_v2 = pct_emps_psl_pto_state
	cap g pct_emps_psl_pto_place_v2 = pct_emps_psl_pto_place
	cap g pct_emps_psl_ptoexc_state_v2 = pct_emps_psl_ptoexc_state
	cap g pct_emps_psl_ptoexc_place_v2 = pct_emps_psl_ptoexc_place
	
	cap g pct_ests_psl_state_v2 = pct_ests_psl_state
	cap g pct_ests_psl_place_v2 = pct_ests_psl_place
	cap g pct_ests_psl_pto_state_v2 = pct_ests_psl_pto_state
	cap g pct_ests_psl_pto_place_v2 = pct_ests_psl_pto_place
	cap g pct_ests_psl_ptoexc_state_v2 = pct_ests_psl_ptoexc_state
	cap g pct_ests_psl_ptoexc_place_v2 = pct_ests_psl_ptoexc_place
	

*For all regressions: pct_emps = percent of employees, pct_ests = percent of establishments

*Share in a PSL Covered State (treating ME, NV, ME as not having PSL)
eststo col1row1: mi est, post: reg benefits_paidsick c.pct_emps_psl_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_law_states_hybd == 0, vce(cluster statelist)
eststo col1row2: mi est, post: reg benefits_paidsick c.pct_ests_psl_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_law_states_hybd == 0, vce(cluster statelist) 
eststo col1row3: mi est, post: reg benefits_paidsick c.pct_emps_psl_place_v2 $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_law_states_hybd == 0, vce(cluster statelist) 
eststo col1row4: mi est, post: reg benefits_paidsick c.pct_ests_psl_place_v2 $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_law_states_hybd == 0, vce(cluster statelist) 
	local n1 = e(N)

		eststo col1: appendmodels col1row1 col1row2 col1row3 col1row4
		estadd local samplesize "`n1'": col1
		

*Share in a PSL Covered State or County (treating ME, NV, ME as not having PSL)
eststo col2row5: mi est, post: reg benefits_paidsick c.pct_emps_psl_pto_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
eststo col2row6: mi est, post: reg benefits_paidsick c.pct_ests_psl_pto_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
eststo col2row7: mi est, post: reg benefits_paidsick c.pct_emps_psl_pto_place_v2 $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
eststo col2row8: mi est, post: reg benefits_paidsick c.pct_ests_psl_pto_place_v2 $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
	local n2 = e(N)

		eststo col2: appendmodels col2row5 col2row6 col2row7 col2row8
		estadd local samplesize "`n2'": col2

*Share in a PSL Covered State (treating ME, NV, ME as having PSL)
eststo col3row9: mi est, post: reg benefits_paidsick c.pct_emps_psl_ptoexc_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
eststo col3row10: mi est, post: reg benefits_paidsick c.pct_ests_psl_ptoexc_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
eststo col3row11: mi est, post: reg benefits_paidsick c.pct_emps_psl_ptoexc_place_v2 $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
eststo col3row12: mi est, post: reg benefits_paidsick c.pct_ests_psl_ptoexc_place_v2 $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_states_hybd == 0, vce(cluster statelist) 
	local n3 = e(N)

		eststo col3: appendmodels col3row9 col3row10 col3row11 col3row12
		estadd local samplesize "`n3'": col3

*Share in a PSL Covered State or County (treating ME, NV, ME as having PSL)
eststo col4row1: mi est, post: reg benefits_paidsick c.pct_emps_psl_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_law_places_hybd == 0, vce(cluster statelist) 
eststo col4row2: mi est, post: reg benefits_paidsick c.pct_ests_psl_state $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_law_places_hybd == 0, vce(cluster statelist) 
eststo col4row3: mi est, post: reg benefits_paidsick c.pct_emps_psl_place_v2 $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_law_places_hybd == 0, vce(cluster statelist) 
eststo col4row4: mi est, post: reg benefits_paidsick c.pct_ests_psl_place_v2 $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_law_places_hybd == 0, vce(cluster statelist) 
	local n4 = e(N)

		eststo col4: appendmodels col4row1 col4row2 col4row3 col4row4
		estadd local samplesize "`n4'": col4

*Share in a PSL Covered State (ignoring ME, NV, ME)
eststo col5row5: mi est, post: reg benefits_paidsick c.pct_emps_psl_pto_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
eststo col5row6: mi est, post: reg benefits_paidsick c.pct_ests_psl_pto_state $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
eststo col5row7: mi est, post: reg benefits_paidsick c.pct_emps_psl_pto_place_v2 $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
eststo col5row8: mi est, post: reg benefits_paidsick c.pct_ests_psl_pto_place_v2 $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
	local n5 = e(N)

		eststo col5: appendmodels col5row5 col5row6 col5row7 col5row8
		estadd local samplesize "`n5'": col5


*Share in a PSL Covered State or County (ignoring ME, NV, ME)
eststo col6row9: mi est, post: reg benefits_paidsick c.pct_emps_psl_ptoexc_state $demog $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
eststo col6row10: mi est, post: reg benefits_paidsick c.pct_ests_psl_ptoexc_state $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
eststo col6row11: mi est, post: reg benefits_paidsick c.pct_emps_psl_ptoexc_place_v2 $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist) 
eststo col6row12: mi est, post: reg benefits_paidsick c.pct_ests_psl_ptoexc_place_v2 $demog  $work $statechars $empchars i.statelist i.year##i.month if  psl_pto_law_places_hybd == 0, vce(cluster statelist)
	local n6 = e(N)

		eststo col6: appendmodels col6row9 col6row10 col6row11 col6row12
		estadd local samplesize "`n6'": col6

		
		

***********
**** EXPORT 
***********

*Creating labels for regressions in latex
label var pct_emps_psl_state "\hspace{0.5cm} Percent of Workers"
label var pct_ests_psl_state "\hspace{0.5cm} Percent of Establishments"
label var pct_emps_psl_place_v2 "\hspace{0.5cm} Percent of Workers"
label var pct_ests_psl_place_v2 "\hspace{0.5cm} Percent of Establishments"

label var pct_emps_psl_pto_state "\hspace{0.5cm} Percent of Workers"
label var pct_ests_psl_pto_state "\hspace{0.5cm} Percent of Establishments"
label var pct_emps_psl_pto_place_v2 "\hspace{0.5cm} Percent of Workers"
label var pct_ests_psl_pto_place_v2 "\hspace{0.5cm} Percent of Establishments"

label var pct_emps_psl_ptoexc_state "\hspace{0.5cm} Percent of Workers"
label var pct_ests_psl_ptoexc_state "\hspace{0.5cm} Percent of Establishments"
label var pct_emps_psl_ptoexc_place_v2 "\hspace{0.5cm} Percent of Workers"
label var pct_ests_psl_ptoexc_place_v2 "\hspace{0.5cm} Percent of Establishments"

estadd local check1 "\checkmark": col1 col4
estadd local check2 "\checkmark": col2 col5
estadd local check3 "\checkmark": col3 col6




esttab col1 col2 col3 col4 col5 col6 using "$exhibits_path\Appendix Table 3.tex", ///
	keep(*psl*) label mgroups("\shortstack{PSL Access in \\ Non-Mandate States}" "\shortstack{PSL Access in \\ Non-Mandate States/Counties}", pattern(1 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	refcat(pct_emps_psl_state "\textit{Share in PSL Covered State}" pct_emps_psl_place_v2 "\textit{Share in PSL Covered State or County}" pct_emps_psl_pto_state "\\ \textit{Share in PSL Covered State}" pct_emps_psl_pto_place_v2 "\textit{Share in PSL Covered State or County}" pct_emps_psl_ptoexc_state "\\ \textit{Share in PSL Covered State}" pct_emps_psl_ptoexc_place_v2 "\textit{Share in PSL Covered State or County}", nolabel) ///
	mtitles("M1" "M2" "M3" "M4" "M5" "M6") ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	scalars("check1 Maine/Michigan/Nevada = no PSL" "check2 Maine/Michigan/Nevada = PSL" "check3 Maine/Michigan/Nevada = dropped" "line \hline \vspace{-1em}" "samplesize N") ///
	noomitted noconstant nonumbers nonote noobs not nodepvars eqlabels(none) ///
	b(%9.3f) replace